/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.pos.services.impl;

import com.pos.domain.Customer;
import com.pos.domain.Item;
import com.pos.domain.ItemCategory;
import com.pos.domain.PurchaseDetails;
import com.pos.domain.SalesDetail;
import com.pos.domain.Vendor;
import com.pos.ejb.CategoryEJB;
import com.pos.ejb.ItemEJB;
import com.pos.ejb.ItemTradingEJB;
import com.pos.ejb.VendorEJB;
import com.pos.services.BaseService;
import com.pos.services.CustomerService;
import java.util.List;
import javax.inject.Named;
import javax.ejb.EJB;
import javax.inject.Inject;

/**
 *
 * @author Dipendra
 */
@Named
public class BaseServiceImpl implements BaseService {

    @EJB
    VendorEJB supplierEJB;
    @EJB
    ItemEJB productEJB;
    @EJB
    CategoryEJB categoryEJB;
    @EJB
    ItemTradingEJB tradingEJB;
    @Inject
    CustomerService customerService;

    // Suppliers
    //-------------------------------------------
    public List<Vendor> findAllSupplier() {
        return supplierEJB.findAll();
    }

    public void addSupplier(Vendor supplier) {
        supplierEJB.create(supplier);
    }

    public Vendor findSupplierById(Object id) {
        return supplierEJB.find(id);
    }

    public void removeSupplier(Vendor supplier) {
        supplierEJB.remove(supplier);
    }

    public void saveSupplier(Vendor supplier) {
        if (supplier.getId() <= 0 || supplierEJB.find(supplier.getId()) == null) {
            supplierEJB.create(supplier);
        } else {
            supplierEJB.edit(supplier);
        }
    }

    // Products
    //------------------------------------------
    public List<Item> findAllProduct() {
        return productEJB.findAll();
    }

    public Item findProductById(Object id) {
        return productEJB.find(id);
    }

    public void saveProduct(Item product) {
        if (product.getId() <= 0 || productEJB.find(product.getId()) == null) {
            productEJB.create(product);
        } else {
            productEJB.edit(product);
        }
    }

    public List<ItemCategory> findAllCatagory() {
        return categoryEJB.findAllCategory();
    }

    public void saveCategory(ItemCategory category) {
        if (category.getId() <= 0 || findCategoryById(category.getId()) == null) {
            categoryEJB.create(category);
        } else {
            categoryEJB.edit(category);
        }
    }

    public ItemCategory findCategoryById(Object id) {
        return categoryEJB.find(id);
    }

    public void purchaseItem(PurchaseDetails details) {
        this.tradingEJB.savePurchase(details);
    }

    public void saleItem(SalesDetail detail) {
        if (detail.getCustomer() == null) {
            detail.setCustomer(this.customerService.getDefault());
        }
        this.tradingEJB.saveSale(detail);

    }

    public List<Customer> findAllCustomer() {
        return this.customerService.findAll();
    }
}
